<template>
  <div class="body">
    <h1>B站AV与BV互转</h1>
    <el-input placeholder="请输入AV或BV号" v-model="input">
      <template slot="prepend">AV或BV号</template>
      <el-button
        slot="append"
        icon="el-icon-search"
        @click="search"
      ></el-button>
    </el-input>
    <el-input
      type="textarea"
      :rows="2"
      placeholder="输出的结果"
      v-model="result"
      style="padding-top:25px;"
    >
    </el-input>
    <iframe
      :src="href"
      scrolling="no"
      border="0"
      frameborder="no"
      framespacing="0"
      allowfullscreen="true"
      style="padding-top:25px;"
    >
    </iframe>
    <ul>
      <li><a href="https://www.zhihu.com/question/381784377/answer/1099438784" target="_blank" rel="noopener">算法来源</a></li>
      <li><a href="https://cloudhao.top" target="_blank" rel="noopener">个人博客</a></li>
    </ul>
    <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      input: "",
      result: "",
      aid: "av170001",
      bid: "BV17x411w7KC",
      href: this.href = `//player.bilibili.com/player.html?aid=av170001&bvid=BV17x411w7KC&page=1`
    };
  },
  methods: {
    search() {
      let table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
      let tr = new Object();
      for (let i = 0; i < 58; i++) {
        tr[table[i]] = i;
      }
      let s = [11, 10, 3, 8, 4, 6];
      let xor = 177451812;
      let add = 8728348608;
      if (this.input.slice(0, 2).toLowerCase() === "bv") {
        this.bid = this.input;
        let r = 0;
        for (let i = 0; i < 6; i++) {
          r += tr[this.input[s[i]]] * 58 ** i;
        }
        this.result = "av" + ((r - add) ^ xor).toString();
        this.aid = ((r - add) ^ xor).toString();
      } else if (this.input.slice(0, 2).toLowerCase() === "av") {
        this.aid = this.input.toString();
        let input = this.input.slice(2, this.input.length);
        console.log(input);
        input = (input ^ xor) + add;
        let r = "BV1  4 1 7  ".split("");
        for (let i = 0; i < 6; i++) {
          r[s[i]] = table[Math.floor(input / 58 ** i) % 58];
        }
        this.result = r.join("");
        this.bid = this.result;
      } else{
        this.$message({
          message: '格式有误，试着输入av170001或BV17x411w7KC',
          type: 'warning'
        });
      }
      this.href = `//player.bilibili.com/player.html?aid=${this.aid}&bvid=${this.bid}&page=1`;
    }
  }
};
</script>

<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
.body {
  padding: 0 20px;
}
</style>
